package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.turing.post.organization.util.VoucherTransferOrderDetail;
import

com.turing.post.organization.util.VoucherTransferOrderDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherTransferOrderDetailDaoImpl")
@Remote
@Local
public class VoucherTransferOrderDetailDaoImpl implements
		VoucherTransferOrderDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证调拨单明细列表
	 * 
	 * @param transferOrderId
	 *            调拨单ID
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrderDetail> getVoucherTransferOrderDetailListByTransferOrderId(
			PCPSWebInput webInput, String transferOrderId, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(transferOrderId))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherTransferOrderDetail where TransferOrderId=?1 order by SeqNo";
		Query query = _em.createNativeQuery(sql,
				VoucherTransferOrderDetail.class).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		query.setParameter(1, transferOrderId);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证调拨单明细记录数
	 * 
	 * @param transferOrderId
	 *            调拨单ID
	 * @return 凭证调拨单记录数
	 */
	@Override
	public BigInteger getVoucherTransferOrderDetailListByTransferOrderIdTotal(
			PCPSWebInput webInput, String transferOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return new BigInteger("0");
		String sql = "select nvl(count(*),0)  from VoucherTransferOrderDetail  where TransferOrderId=?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, transferOrderId);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 新增凭证调拨单明细
	 * 
	 * @param VoucherTransferOrderDetail
	 *            凭证调拨单明细
	 * @throws PCPSApplicationException
	 */
	@Override
	public void insertVoucherTransferOrderDetail(
			VoucherTransferOrderDetail voucherTransferOrderDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherTransferOrderDetail);
	}

	/**
	 * 根据条件查询凭证调拨单明细列表
	 * 
	 * @param transferOrderId
	 *            调拨单ID
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherTransferOrderDetail> getVoucherTransferOrderDetailListByTransferOrderIdNotPage(
			PCPSWebInput webInput, BigInteger transferOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| transferOrderId == null)
			return null;
		String sql = "select * from VoucherTransferOrderDetail where TransferOrderId=?1 order by SeqNo";
		Query query = _em.createNativeQuery(sql,
				VoucherTransferOrderDetail.class);
		query.setParameter(1, transferOrderId);
		return query.getResultList();
	}
}
